package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jianshuge.app.AppException;
import com.jianshuge.app.bean.Comment.Refer;
import com.jianshuge.app.bean.Comment.Reply;
import com.jianshuge.app.common.StringUtils;

public class MyBooklistDetail extends Entity {

	private  String title;
	private	 String keywords;
	private	 String descs;
	private	 int user_id;
	private	 String user_name;
	private	 String user_avatar;
	private	 String books_count;
	private	 String views_count;
	private	 String followers_count;
	private	 String comments_count;
	private	 String vote_up_count;
	private	 String create_time;
	private	 String update_time;
	private  boolean follow_status;
	

	
	private List<Reply> replies = new ArrayList<Reply>();
	private List<MyBooklistItem> my_booklist_items = new ArrayList<MyBooklistItem>();
	
	public static class Reply implements Serializable{
		public int id;
		public int item_id;
		public int user_id;
		public String user_name;
		public String user_avatar;
		public String content;
		public String create_time;
	};
	
	public static class MyBooklistItem implements Serializable{
		public int id;
		public int book_id;
		public int booklist_id;
		
		public String book_title;
		public String book_image_url;
		public String book_rating;
		public String subject_id;
		
		public int user_id;
		public String user_name;
		public String user_avatar;
		public String user_summary;
		public String user_comment;

		public int comments_count;
		public int vote_up_count;
		public String update_time;
		public boolean collection_status;
		public List<Reply> replies = new ArrayList<Reply>();
		
		
		public final static int CLIENT_MOBILE = 2;
		public final static int CLIENT_ANDROID = 3;
		public final static int CLIENT_IPHONE = 4;
		public final static int CLIENT_WINDOWS_PHONE = 5;
		private int appClient = 1;
		
		public int getAppClient() {
			return appClient;
		}
		public void setAppClient(int appClient) {
			this.appClient = appClient;
		}
	}
	

	public List<MyBooklistItem> getMyBooklistItems() {
		return my_booklist_items;
	}
	
	public List<Reply> getReplies() {
		return replies;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getKeywords() {
		return keywords;
	}
	
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	
	public String getDescs() {
		return descs;
	}
	
	public void setDescs(String descs) {
		this.descs = descs;
	}

	public String getCommentsCount() {
		return comments_count;
	}
	
	public void setCommentsCount(String commentsCount) {
		this.comments_count = commentsCount;
	}
	
	public int getUserId() {
		return user_id;
	}
	
	public void setUserId(int userId) {
		this.user_id = userId;
	}
	public String getUserName() {
		return user_name;
	}
	
	public void setUserName(String user_name) {
		this.user_name = user_name;
	}
	
	public String getUserAvatar() {
		return user_avatar;
	}
	
	public void setUserAvatar(String userAvatar) {
		this.user_avatar = userAvatar;
	}
	
	public String getBooksCount() {
		return books_count;
	}
	
	public void setBooksCount(String booksCount) {
		this.books_count = booksCount;
	}
	
	public String getViewsCount() {
		return views_count;
	}
	
	public void setViewsCount(String viewsCount) {
		this.views_count = viewsCount;
	}
	
	public String getFollowersCount() {
		return followers_count;
	}
	
	public void setFollowersCount(String followersCount) {
		this.followers_count = followersCount;
	}
	
	public String getVoteUpCount() {
		return vote_up_count;
	}
	
	public void setVoteUpCount(String voteUpCount) {
		this.vote_up_count = voteUpCount;
	}
	
	public String getCreateTime() {
		return create_time;
	}
	
	public void setCreateTime(String createTime) {
		this.create_time = createTime;
	}
	
	public String getUpdateTime() {
		return update_time;
	}
	
	public void setUpdateTime(String updateTime) {
		this.update_time = updateTime;
	}
	
	public boolean getFollowStatus() {
		return follow_status;
	}
	
	public void setFollowStatus(boolean status) {
		this.follow_status = status;
	}
	
	public static MyBooklistDetail parse(InputStream inputStream) throws IOException, AppException {
		
		MyBooklistDetail my_booklist_detail = new MyBooklistDetail();

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
			JSONObject booklist_detail_json_obj = new JSONObject(strResult);
			
			my_booklist_detail.id = booklist_detail_json_obj.getInt("id");
			my_booklist_detail.setTitle(booklist_detail_json_obj.getString("title"));
			my_booklist_detail.setKeywords(booklist_detail_json_obj.getString("keywords"));
			my_booklist_detail.setDescs(booklist_detail_json_obj.getString("descs"));
			my_booklist_detail.setUserId(booklist_detail_json_obj.optInt("user_id"));
			my_booklist_detail.setUserName(booklist_detail_json_obj.getString("user_name"));
			my_booklist_detail.setUserAvatar(booklist_detail_json_obj.getString("user_avatar"));
			my_booklist_detail.setBooksCount(booklist_detail_json_obj.getString("books_count"));
			my_booklist_detail.setViewsCount(booklist_detail_json_obj.getString("views_count"));
			my_booklist_detail.setFollowersCount(booklist_detail_json_obj.getString("followers_count"));
			my_booklist_detail.setCommentsCount(booklist_detail_json_obj.getString("comments_count"));
			my_booklist_detail.setVoteUpCount(booklist_detail_json_obj.getString("vote_up_count"));
			my_booklist_detail.setCreateTime(booklist_detail_json_obj.getString("create_time"));
			my_booklist_detail.setUpdateTime(booklist_detail_json_obj.getString("update_time"));
			my_booklist_detail.follow_status = booklist_detail_json_obj.getBoolean("follow_status");
			
			JSONArray reply_json_arr = booklist_detail_json_obj.optJSONArray("replys");
			if(reply_json_arr != null) {
				for (int j = 0; j < reply_json_arr.length(); j++) {
					JSONObject reply_json_obj = reply_json_arr.getJSONObject(j);
					Reply reply = new Reply();
					reply.id = StringUtils.toInt(reply_json_obj.optString("id"), 0);
					reply.item_id = 0;
					reply.user_id = reply_json_obj.optInt("user_id");
					reply.user_name = reply_json_obj.optString("user_name");
					reply.user_avatar = reply_json_obj.optString("user_avatar");
					reply.content = reply_json_obj.optString("content");
					reply.create_time = reply_json_obj.optString("create_time");
					my_booklist_detail.replies.add(reply);
				}
			}
			
			JSONArray booklistitem_json_arr = booklist_detail_json_obj.optJSONArray("booklistitems");
			if(booklistitem_json_arr != null) {
				for (int k = 0; k < booklistitem_json_arr.length(); k++) {
				
					JSONObject booklist_item_json_obj = booklistitem_json_arr.optJSONObject(k);
					MyBooklistItem booklist_item = new MyBooklistItem();
					booklist_item.id = StringUtils.toInt(booklist_item_json_obj.getString("id"), 0);
					booklist_item.book_id = booklist_item_json_obj.optInt("book_id") ;
					booklist_item.book_title = booklist_item_json_obj.optString("book_title") ;
					booklist_item.book_image_url = booklist_item_json_obj.optString("book_image_url");
					booklist_item.book_rating =  booklist_item_json_obj.optString("book_rating") ;
					booklist_item.subject_id =  booklist_item_json_obj.optString("subject_id") ;
					
					booklist_item.user_id = booklist_item_json_obj.optInt("user_id");
					booklist_item.user_name = booklist_item_json_obj.optString("user_name");
					booklist_item.user_avatar = booklist_item_json_obj.optString("user_avatar");
					booklist_item.user_summary = booklist_item_json_obj.optString("user_summary");
					booklist_item.user_comment = booklist_item_json_obj.optString("user_comment");
					
					booklist_item.comments_count = booklist_item_json_obj.optInt("comments_count");
					booklist_item.vote_up_count = booklist_item_json_obj.optInt("vote");
					booklist_item.update_time = booklist_item_json_obj.optString("update_time");
					booklist_item.collection_status = booklist_item_json_obj.optBoolean("collection_status");
					
					JSONArray booklistitem_reply_json_arr = booklist_item_json_obj.optJSONArray("replys");
					if(reply_json_arr != null) {
						for (int j = 0; j < booklistitem_reply_json_arr.length(); j++) {
							JSONObject reply_json_obj = booklistitem_reply_json_arr.getJSONObject(j);
							Reply reply = new Reply();
							reply.id = StringUtils.toInt(reply_json_obj.optString("id"), 0);
							reply.item_id = booklist_item.id;
							reply.user_id = reply_json_obj.optInt("user_id");
							reply.user_name = reply_json_obj.optString("user_name");
							reply.user_avatar = reply_json_obj.optString("user_avatar");
							reply.content = reply_json_obj.optString("content");
							reply.create_time = reply_json_obj.optString("create_time");
							booklist_item.replies.add(reply);
						}
					}
					
					my_booklist_detail.getMyBooklistItems().add(booklist_item);
				}
			}
			
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
	    return my_booklist_detail;
	}
}
